<?php


use Biz\cache\activity\CActivityIKEA;

/**
 * @deprecated
 */
class IKEAVoteAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $uid = $this->uid;
        $vote = $this->getPost('vote', '0');
        $id = $this->getPost('id', 0);
        $isVote = $uid ? CActivityIKEA::isVote($uid) : 0;
        if ($vote == 1) {
            //投票
            if (in_array($id, [1, 2]) && $isVote == 0) {
                if ($uid > 0) {
                    CActivityIKEA::userVote($uid, $id);
                } else {
                    CActivityIKEA::visitorVote(\Doraemon\tools\HhzClient::getDid(), $id);
                }
                CActivityIKEA::voteRecord($id);
            }
            $this->setSuccess();
            return false;
        }
        //投票结果
        $source = CActivityIKEA::voteRanking();
        $like = intval($source[1] ?? 0);
        $unlike = intval($source[2] ?? 0);
        if ($like == 0 && $unlike == 0) {
            $like = 1;
            $unlike = 1;
        }
        $total = $like + $unlike;
        $likeRate = $total ? round($like / $total * 100) : 0;
        $unlikeRate = 100 - $likeRate;
        $rate = [
            'vote' => [
                ['id' => 1, 'rate' => $likeRate],
                ['id' => 2, 'rate' => $unlikeRate],
            ],
            'is_vote' => $isVote > 0,
            'source' => $source
        ];
        $this->setSuccess($rate);
    }
}